package com.ayu.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ayu.shortlink.project.common.convention.result.Result;
import com.ayu.shortlink.project.common.convention.result.Results;
import com.ayu.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import com.ayu.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.ayu.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.ayu.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.ayu.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import com.ayu.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.ayu.shortlink.project.dto.resp.ShortLinkGroupCountRespDTO;
import com.ayu.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.ayu.shortlink.project.handler.CustomBlockHandler;
import com.ayu.shortlink.project.service.ShortLinkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    @GetMapping("/{short_uri}")
    public void restoreUrl(@PathVariable("short_uri") String shortUri, ServletRequest request, ServletResponse response){
        shortLinkService.restoreUrl(shortUri,request,response);
    }

    @GetMapping("/v2/{short_uri}")
    public void restoreUrl2(@PathVariable("short_uri") String shortUri, ServletRequest request, ServletResponse response){
        shortLinkService.restoreUrl2(shortUri,request,response);
    }

    @GetMapping("/v3/{short_uri}")
    public void restoreUrl3(@PathVariable("short_uri") String shortUri,ServletRequest request,ServletResponse response){
        shortLinkService.restoreUrl3(shortUri,request,response);
    }

    @GetMapping("/v4/{short_uri}")
    public void restoreUrl4(@PathVariable("short_uri") String shortUri,ServletRequest request,ServletResponse response){
        shortLinkService.restoreUrl4(shortUri,request,response);
    }

    /**
     * 创建单个短链接 V1版本
     */
    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
        return Results.success(shortLinkService.create(requestParam));
    }

    /**
     * 创建单个短链接 V2版本
     */
    @PostMapping("/api/short-link/v2/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> createShortLink2(@RequestBody ShortLinkCreateReqDTO requestParam){
        return Results.success(shortLinkService.create2(requestParam));
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> createShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam){
        return Results.success(shortLinkService.batchCreat(requestParam));
    }

    /**
     * 短链接查询分页
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        return Results.success(shortLinkService.getPage(requestParam));
    }

    /**
     * 查询短链接分组内数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam){
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
    }

    /**
     * 更新短链接信息
     */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }
}
